// Project: fcounter_2pic2550
// FileName: fcounter_2pic2550
#include <p18cxxx.h>
#include <delays.h>
#include <timers.h>
#pragma config FOSC=HSPLL_HS, PLLDIV=5, CPUDIV=OSC1_PLL2
#pragma config USBDIV=2, PWRT=OFF, FCMEN=OFF, IESO=OFF
#pragma config BOR=OFF, BORV=2, WDT=OFF, LVP=OFF, VREGEN=OFF
#pragma config MCLRE=OFF, PBADEN=OFF, WDTPS=1024
#pragma config LPT1OSC=OFF, CCP2MX=OFF, DEBUG=OFF
#pragma config CP0=OFF, CP1=OFF, CP2=OFF, CP3=OFF, CPB=OFF
#pragma config CPD=OFF, WRT0=OFF, WRT1=OFF, WRT2=OFF, WRT3=OFF
#pragma config WRTB=OFF, WRTC=OFF, WRTD=OFF, EBTR0=OFF
#pragma config EBTR1=OFF, EBTR2=OFF, EBTR3=OFF, EBTRB=OFF
#define lcd_DB LATB
#define lcd_E LATBbits.LATB3
#define lcd_RS LATAbits.LATA3
#define Delay_50uS Delay100TCYx(6)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_100mS Delay10KTCYx(120)
// double ww, w;
unsigned long FFF, FF, F;
unsigned long A, AA, TH, TL;
unsigned long aa, bb, cc, dd;
char frq0[] = "f (MHz)";
char frq1[] = "f Low(kHz)";
char frqP[] = "fPrescaler(MHz)";
char Hz[] = "(MHz)";
void lcd_w4(char asci);
void lcd_c4(char cmd);
void lcd_clr(void);
void lcd_init(void);
void lcd_puts(char *s);
void lcd_locate(char y,char x);
void lcd_putf(unsigned long f);
void lcd_putfLow(unsigned long f);
void lcd_putfPre(unsigned long f);
void UserInit(void);
void lcd_Init(void);
void high(void);
void low(void);
#pragma interrupt high
#pragma interruptlow low
#pragma code H_vect = 0x0008
void H_isr(void){
_asm goto high _endasm
}
#pragma code L_vect = 0x0018
void L_isr(void){
_asm goto low _endasm
}
#pragma code
void high(){
if(INTCONbits.TMR0IF){
A++;
INTCONbits.TMR0IF = 0;
}
}
void low(){
if(INTCON3bits.INT1IF){
T0CONbits.TMR0ON = 0;
Delay1TCY();
TL = TMR0L; TH = TMR0H;
AA = A;
A = 0; TMR0H = 0; TMR0L = 0;
PORTCbits.RC1 = 0;
Delay1TCY();Delay1TCY();
PORTCbits.RC1 = 1;
PORTCbits.RC7 = 0;
Delay1TCY();Delay1TCY();
PORTCbits.RC7 = 1;
aa = 1; bb = 1; cc = 1; dd = 1;
if(PORTAbits.RA0) aa = 0;
if(PORTAbits.RA1) bb = 0;
if(PORTAbits.RA2) cc = 0;
if(PORTCbits.RC6) dd = 0;
FFF = (AA*65536 + TH*256 + TL)*16 \
+ dd*8 + cc*4 + bb*2 + aa;
INTCON3bits.INT1IF = 0;
PORTAbits.RA5 = 1;
Delay1TCY();
T0CONbits.TMR0ON = 1;
PORTAbits.RA5 = 0;
Delay1TCY();
}
}
void lcd_w4(char c){
lcd_DB = (c & 0xF0) | (lcd_DB & 0x0F);
lcd_E = 1;
Delay10TCYx(20);
lcd_E = 0;
}
void lcd_c4(char c){
lcd_RS = 0;
lcd_w4(c);
lcd_w4(c<<4);
if(c & 0xFC)
Delay_50uS;
else
Delay_2mS;
}
void lcd_clr(void){
lcd_c4(0x01);
}
void lcd_locate(char y, char x){
unsigned char p;
switch(y & 0x03){
case 0: p=0x80; break;
case 1: p=0xC0; break;
case 2: p=0x94; break;
case 3: p=0xD4; break;
}
lcd_c4(p += x);
}
void lcd_putchr(char c){
lcd_RS = 1;
lcd_w4(c);
lcd_w4(c<<4);
Delay_50uS;
}
void lcd_puts(char *s){
while(*s)
lcd_putchr(*s++);
}
void lcd_putf(
unsigned long f){
unsigned char i;
unsigned char buf[10];
for(i=0; i<10; i++) buf[i] = ' ';
i = 9;
do{
buf[i] = (f % 10) + '0';
f = f / 10;
i--;
} while(f > 0);
lcd_putchr(buf[2]);
lcd_putchr(buf[3]);
lcd_putchr('.');
for(i=4; i<10; i++) lcd_putchr(buf[i]);
}
void lcd_putfLow(
unsigned long f){
unsigned char i;
unsigned char buf[10];
for(i=0; i<10; i++) buf[i] = ' ';
i = 9;
do{
buf[i] = (f % 10) + '0';
f = f / 10;
i--;
} while(f > 0);
for(i=2; i<7; i++) lcd_putchr(buf[i]);
lcd_putchr('.');
for(i=7; i<10; i++) lcd_putchr(buf[i]);
}
void lcd_putfPre(
unsigned long f){
unsigned char i;
unsigned char buf[10];
f = f * 32;
for(i=0; i<10; i++) buf[i] = ' ';
i = 9;
do{
buf[i] = (f % 10) + '0';
f = f / 10;
i--;
} while(f > 0);
for(i=1; i<4; i++) lcd_putchr(buf[i]);
lcd_putchr('.');
for(i=4; i<9; i++) lcd_putchr(buf[i]);
}
void lcd_init(void){
Delay_20mS;
lcd_RS = 0;
lcd_w4(0x30);Delay_5mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x20);Delay_1mS;
lcd_c4(0x2E);
lcd_c4(0x08);
lcd_c4(0x0C);
lcd_c4(0x06);
lcd_c4(0x01);
}
void UserInit(void){
FFF = 0; FF = 1;
TMR0H = 0; TMR0L = 0; A = 0;
PORTBbits.RB0 = 0;
PORTCbits.RC1 = 1;
PORTCbits.RC7 = 1;
}
void main(void){
ADCON1 = 0x0F;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTE = 0x00;
TRISA = 0b00010111;
TRISB = 0b00000111;
TRISC = 0b01000101;
OpenTimer0(
TIMER_INT_ON &
T0_16BIT &
T0_PS_1_1 &
T0_SOURCE_EXT &
T0_EDGE_RISE
);
INTCON2bits.TMR0IP = 1;
INTCONbits.TMR0IF = 0;
INTCON3bits.INT1IE = 1;
INTCON3bits.INT1IP = 0;
INTCON3bits.INT1IF = 0;
RCONbits.IPEN = 1;
INTCONbits.GIEH = 1;
INTCONbits.GIEL = 1;
UserInit();
lcd_init();
while(1){
// w = FFF;
// ww = w*1.0000004167;
// F = ww;
if(FF != FFF){
FF = FFF;
if(PORTCbits.RC0 == 0){
lcd_clr();
lcd_locate(0, 1);
lcd_puts(frq0);
lcd_locate(1, 4);
lcd_putf(FF);
}
if(PORTCbits.RC2 == 0){
lcd_clr();
lcd_locate(0, 1);
lcd_puts(frqP);
lcd_locate(1, 4);
lcd_putfPre(FF);
}
if(PORTEbits.RE3 == 0){
lcd_clr();
lcd_locate(0, 1);
lcd_puts(frq0);
lcd_locate(1, 4);
lcd_putf(FF);
}
}
Delay_10mS;
}
}